package IT.Demo.Web.Servlet;


import IT.Demo.Pojo.Brand;
import IT.Demo.Pojo.Pagebean;
import IT.Demo.Service.Brandservice;
import IT.Demo.Service.impl.Brandserviceimpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{


    private Brandservice service = new Brandserviceimpl();


    //查询所有数据
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<Brand> brands = service.selectAll();

        String jsonString = JSON.toJSONString(brands);

        response.setContentType("text/json;charset=utf-8");

        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException{


        BufferedReader reader = request.getReader();

        String s = reader.readLine();


        Brand brands = JSON.parseObject(s, Brand.class);

        service.add(brands);

        response.getWriter().write("success");

    }


    public void deleteByids(HttpServletRequest request, HttpServletResponse response) throws IOException{
        BufferedReader reader = request.getReader();

        String params = reader.readLine();

        int[] ids = JSON.parseObject(params, int[].class);

        service.deletByids(ids);

        response.getWriter().write("success");

    }


    public void selectByPage(HttpServletRequest request, HttpServletResponse response)throws IOException{

        String _currentPage = request.getParameter("currentPage");

        String _size = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_size);


        Pagebean<Brand> pagebean = service.selectedByPage(currentPage, pageSize);

        String jsonString = JSON.toJSONString(pagebean);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response)throws IOException{

        String _currentPage = request.getParameter("currentPage");
        String _size = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_size);

        BufferedReader reader = request.getReader();

        String params = reader.readLine();

        Brand brand = JSON.parseObject(params, Brand.class);


        Pagebean<Brand> pagebean = service.selectBypageAndcondition(currentPage,pageSize,brand);

        String jsonString = JSON.toJSONString(pagebean);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }




}
